//
//  XLViewController.m
//  XLConversationInputView
//
//  Created by 10150820867@qq.com on 08/02/2022.
//  Copyright (c) 2022 10150820867@qq.com. All rights reserved.
//

#define WeakSelf(weakSelf)              __weak __typeof(&*self) weakSelf = self;
#define StrongSelf(strongSelf)          __strong __typeof(&*self) strongSelf = weakSelf;

#import "XLViewController.h"
#import <XLConversationInputView/XLConversationInputView.h>
#import <AVFoundation/AVFoundation.h>



typedef NS_ENUM(NSInteger, DemoItemType) {
    /// @
    DemoItemType_Album = 2300,
    /// @
    DemoItemType_Voice_Call,
    /// @
    DemoItemType_Video_Call,
    /// @
    DemoItemType_File,
    /// @
    DemoItemType_Link,
    /// @
    DemoItemType_Applets,
    /// @
    DemoItemType_Business_Card,
    /// @
    DemoItemType_Location,
    /// @
    DemoItemType_Transfer,
    /// @
    DemoItemType_SyncMessage,
    /// @
    DemoItemType_MarketingMaterialLibrary,
    /// @
    DemoItemType_MaterialLibrary
};

@interface XLViewController ()
<
XLConversationInputViewProtocol,
XLCIFileTypeProtocol
>
@property (nonatomic, assign) CGFloat inputViewHeight;
@property (nonatomic, strong) XLConversationInputView *inputView;
@end

@implementation XLViewController
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.inputView enableKeyboardNotification:YES];
}
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self.inputView enableKeyboardNotification:NO];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.layer.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1.0].CGColor; 
    self.inputView = [[XLConversationInputView alloc] initWithViewController:self];
    self.inputView.maxMessageTextLength = 4000;
    self.inputView.minVoiceDuration = 1;
    self.inputView.maxVoiceDuration = 60;
    self.inputView.inputViewRectBlock = ^(CGRect rect, NSTimeInterval duration) {
        NSLog(@"%lf, %lf, %lf, %lf", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
    };
    [self.view addSubview:self.inputView];
    [self.inputView showInputView];
}
- (void)sendVideo:(PHAsset *)videoAsset andCover:(UIImage *)coverImage
{
    [XLMovEncodeToMpegTool convertMovToMp4FromPHAsset:videoAsset
                        andAVAssetExportPresetQuality:AVAssetExportPresetHighestQuality
                                       andResultBlock:^(NSURL *mp4FileUrl, NSError *error) {
    }];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view.window endEditing:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)fileListAction:(id)sender {
    
    XLFileListViewController *fileList = [[XLFileListViewController alloc] init];
    fileList.delegate = self;
    fileList.sendFileBlock = ^(NSArray<NSDictionary *> *files) {
        NSLog(@"");
    };
    [self.navigationController pushViewController:fileList animated:YES];
}

#pragma mark XLCMoreItemTypeDelegate

- (NSArray <XLCvstInputMoreItemModel *> *)supportMoreItem
{
    return @[
        [[XLCvstInputMoreItemModel alloc] initWithType:DemoItemType_Album
                                             titleText:@"相册"
                                             iconImage:[UIImage imageNamed:@"ToolBar_相册_icon"]
                                        customProperty:nil],
        
        [[XLCvstInputMoreItemModel alloc] initWithType:DemoItemType_Voice_Call
                                             titleText:@"语音通话"
                                             iconImage:[UIImage imageNamed:@"ToolBar_语音通话_icon"]
                                        customProperty:nil],
        
        [[XLCvstInputMoreItemModel alloc] initWithType:DemoItemType_Video_Call
                                             titleText:@"视频通话"
                                             iconImage:[UIImage imageNamed:@"ToolBar_视频通话_icon"]
                                        customProperty:nil],
        
        [[XLCvstInputMoreItemModel alloc] initWithType:DemoItemType_File
                                             titleText:@"文件"
                                             iconImage:[UIImage imageNamed:@"ToolBar_文件_icon"]
                                        customProperty:nil],
        
        [[XLCvstInputMoreItemModel alloc] initWithType:DemoItemType_Link
                                             titleText:@"链接"
                                             iconImage:[UIImage imageNamed:@"ToolBar_链接_icon"]
                                        customProperty:nil],
        
        [[XLCvstInputMoreItemModel alloc] initWithType:DemoItemType_Applets
                                             titleText:@"小程序"
                                             iconImage:[UIImage imageNamed:@"ToolBar_小程序_icon"]
                                        customProperty:nil],
        
        [[XLCvstInputMoreItemModel alloc] initWithType:DemoItemType_Business_Card
                                             titleText:@"名片"
                                             iconImage:[UIImage imageNamed:@"ToolBar_名片_icon"]
                                        customProperty:nil],
        
        [[XLCvstInputMoreItemModel alloc] initWithType:DemoItemType_Location
                                             titleText:@"位置"
                                             iconImage:[UIImage imageNamed:@"ToolBar_位置_icon"]
                                        customProperty:nil],
        
        [[XLCvstInputMoreItemModel alloc] initWithType:DemoItemType_Transfer
                                             titleText:@"好友转接"
                                             iconImage:[UIImage imageNamed:@"ToolBar_转接_icon"]
                                        customProperty:nil],
        
        [[XLCvstInputMoreItemModel alloc] initWithType:DemoItemType_SyncMessage
                                             titleText:@"同步消息"
                                             iconImage:[UIImage imageNamed:@"ToolBar_同步_icon"]
                                        customProperty:nil],
        
        [[XLCvstInputMoreItemModel alloc] initWithType:DemoItemType_MarketingMaterialLibrary
                                             titleText:@"营销雷达"
                                             iconImage:[UIImage imageNamed:@"ToolBar_营销素材库_icon"]
                                        customProperty:nil],
        
        [[XLCvstInputMoreItemModel alloc] initWithType:DemoItemType_MaterialLibrary
                                             titleText:@"素材库"
                                             iconImage:[UIImage imageNamed:@"ToolBar_素材库_icon"]
                                        customProperty:nil]
    ];
}

- (BOOL)selectCompleteWithItem:(XLCvstInputMoreItemModel *)item andValue:(id)value
{
    switch (item.type) {
        case DemoItemType_Album:
        {
             
        }
            break;
        case DemoItemType_Voice_Call:
        {
            
        }
            break;
        case DemoItemType_Video_Call:
        {
            
        }
            break;
        case DemoItemType_File:
        {
            [self.inputView  moreItemAcitonFile:^(NSArray<NSDictionary *> *files) {
                NSLog(@"%@", files);
            }];
        }
            break;
        case DemoItemType_Link:
        {
            
        }
            break;
        case DemoItemType_Applets:
        {
            
        }
            break;
        case DemoItemType_Business_Card:
        {
            
        }
            break;
        case DemoItemType_Location:
        {
            
        }
            break;
        case DemoItemType_Transfer:
        {
            
        }
            break;
        case DemoItemType_SyncMessage:
        {
            
        }
            break;
        case DemoItemType_MarketingMaterialLibrary:
        {
            
        }
            break;
        {
            NSLog(@"");
        }
        default:
            break;
    }
    return YES;
}

#pragma mark fileDelegate

- (NSArray<XLCFileTypeKey> *)supportFileTypes
{
    return @[XLCFileTypeKeyWordDoc,
             XLCFileTypeKeyWordDocx,
             XLCFileTypeKeyExcelXls,
             XLCFileTypeKeyExcelXlsx,
             XLCFileTypeKeyPowerPointPpt,
             XLCFileTypeKeyPowerPointPptx,
             XLCFileTypeKeyZip,
             XLCFileTypeKeyRar,
             XLCFileTypeKeyText,
             XLCFileTypeKeyTxt,
             XLCFileTypeKeyLog,
             XLCFileTypeKeyPdf];
}

- (NSArray<NSString *> *)fileListDirectory
{
    NSMutableArray *fileListDirectoryArray = [NSMutableArray array];
    NSString *path1 = [NSString stringWithFormat:@"%@/Library/Caches/", NSHomeDirectory()];
    [fileListDirectoryArray addObject:path1];
    return fileListDirectoryArray;
}

- (NSDictionary *)audioRecorderParameters
{
    NSDictionary *parameters = @{
        AVFormatIDKey: @(kAudioFormatLinearPCM),
        AVSampleRateKey: @(16000.0),
        AVNumberOfChannelsKey: @(1),
        AVAudioFileTypeKey:@(kAudioFileWAVEType),
        AVEncoderBitDepthHintKey: @(16),
        AVEncoderAudioQualityKey: @(AVAudioQualityHigh)
    };
    return parameters;
}

- (NSString *)audioRecorderCachesDirectory
{
    NSString* cachesDirectory = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    cachesDirectory = [cachesDirectory stringByAppendingPathComponent:[[NSBundle mainBundle] bundleIdentifier]];
    NSString* rootPath = [NSString stringWithFormat:@"%@/VoiceRecoredCache", cachesDirectory];
    return rootPath;
}
@end
